from DAL.sqlHelper import get_keyboard_conn as conn_a

class systemManageDAL:
    def getModule(self,id=0):
        sql ="""
         SELECT * FROM charge1_right_module
        """
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchall()
                return res
        finally:
            conn.close()

    def getAccountByCode(self):
        sql = """
              SELECT  id,right_code,alias,account,token,create_time
              FROM `charge1_account`
              """
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchall()
                return res
        finally:
            conn.close()



    def getAccountByCode(self,right_code=""):
        sql = """
              SELECT  id,right_code,alias,account,token,create_time
              FROM `charge1_account`
              WHERE  right_code='{0}'
              """
        sql = sql.format(right_code)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = cursor.fetchall()
                return res
        finally:
            conn.close()


    def delAccountByCode(self,right_code=""):
        sql = """
              DELETE 
              FROM  `charge1_account`
              WHERE  right_code='{0}'
              """
        sql = sql.format(right_code)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                res = 1
                return res
        finally:
            conn.close()

    def updateAccountToken(self, right_code="",token=""):
        sql = """
            UPDATE  `charge1_account`
            SET  token='{1}'
            WHERE  right_code='{0}'
             """
        sql = sql.format(right_code,token)
        # print(sql)
        try:
            conn = conn_a()
            with conn.cursor() as cursor:
                cursor.execute(sql)
                conn.commit()
                res = 1
                return res
        finally:
            conn.close()





if __name__ == "__main__":
     # 调试部分---------------------------
     dal = systemManageDAL()
     # res = dal.getModule()
     # res = dal.getAccount('f439c9c1-19a4-11ef-8516-00163e0d1539')
     res = dal.updateAccountToken('f439c9c1-19a4-11ef-8516-00163e0d1539','token')
     print(res)